﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using Zero.Core;



namespace Zero.Web.CustomsAttribute
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class PermissionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //是否启用权限控制

            bool IsIgnored = false;
            if (filterContext == null)
            {
                throw new ArgumentException("filterContext");
            }

            //
            var path = filterContext.HttpContext.Request.Path.ToLower();
            IList<string> allowPages = ConfigurationManager.AppSettings["AllowPages"].Split(',').ToList();
            foreach (string page in allowPages)
            {
                if (page.ToLower() == path)
                {
                    IsIgnored = true;
                    break;
                }
            }

            if (IsIgnored)
                return;

            if (SysContext.CurrentUserID == "")
            {
                if (path=="/")
                    filterContext.Result = new RedirectResult("/Account/Login");
                else
                {
                    var obj = new StatusJsonHelper();
                    obj.statusCode = "301";
                    obj.message = "\u4f1a\u8bdd\u8d85\u65f6\uff0c\u8bf7\u91cd\u65b0\u767b\u5f55\u3002";
                    obj.navTabId = "";
                    obj.forwardUrl = "/account/login";
                    obj.callbackType = "";
                    filterContext.Result = new JsonResult { Data = obj, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                }
                base.OnActionExecuted(filterContext);
                // filterContext.RequestContext.HttpContext.Response.Write(JsonConvert.SerializeObject(obj));
            }

        }

    }
}
